package com.mproject.activity.project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.mproject.activity.R;
import com.mproject.activity.TabGroupActivity;
import com.mproject.customclass.Project;
import com.mproject.customclass.Student;
import com.mproject.network.IVLE_API_Helper;
import com.mproject.network.ProgressBarManager;

public class Project_Info_Member_Detail extends Activity implements OnClickListener{

	private TextView back_button;
	private TextView user_name;
	private TextView user_id;
	private TextView user_role;
	private TextView user_email;

	private TextView send_message;
	private TextView set_manager;
	private TextView set_member;
	private TextView remove;

	private Project project;
	private Student student;

	public static SharedPreferences mypref;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.project_info_member_detail); 

		Bundle extras=getIntent().getExtras();
		project=(Project) extras.getSerializable("project");
		student=(Student) extras.getSerializable("member");
		mypref=getSharedPreferences("My_Prefs",Activity.MODE_WORLD_WRITEABLE);

		back_button = (TextView)findViewById(R.id.project_info_member_back);
		user_name = (TextView)findViewById(R.id.project_info_member_user_name);
		user_id = (TextView)findViewById(R.id.project_info_member_user_id);
		user_role = (TextView)findViewById(R.id.project_info_member_user_role);
		user_email = (TextView)findViewById(R.id.project_info_member_user_email);
		send_message = (TextView)findViewById(R.id.project_info_member_send_message);
		set_manager = (TextView)findViewById(R.id.project_info_member_set_manager);
		set_member = (TextView)findViewById(R.id.project_info_member_set_member);
		remove = (TextView)findViewById(R.id.project_info_member_remove);
		send_message.setOnClickListener(this);
		set_manager.setOnClickListener(this);
		set_member.setOnClickListener(this);
		remove.setOnClickListener(this);
		back_button.setOnClickListener(this);

		//set details
		user_name.setText(student.getName());
		user_id.setText(student.getUserID());
		user_email.setText(student.getEmail());

		if (student.isCurrentUser() || student.isOwner()) {
			set_manager.setVisibility(View.GONE);
			set_member.setVisibility(View.GONE);
			remove.setVisibility(View.GONE);
			if (student.isOwner()) 
				user_role.setText("Owner");
			else
				if (project.isManager()) 
					user_role.setText("Manager");
				else
					user_role.setText("Member");
		}
		else {
			if (project.isManager()) {

				remove.setVisibility(View.VISIBLE);
				if (student.isManager()) {
					user_role.setText("Manager");
					set_manager.setVisibility(View.GONE);
					set_member.setVisibility(View.VISIBLE);
				}
				else {
					user_role.setText("Member");
					set_manager.setVisibility(View.VISIBLE);
					set_member.setVisibility(View.GONE);
				}

			}
			else {
				set_manager.setVisibility(View.GONE);
				set_member.setVisibility(View.GONE);
				remove.setVisibility(View.GONE);

				if (student.isManager()) 
					user_role.setText("Manager");
				else
					user_role.setText("Member");
			}

		}

	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == send_message) {
			Intent previewMessage = new Intent(this.getParent(), Project_Message_Detail.class);
			previewMessage.putExtra("student",student);
			previewMessage.putExtra("project",project);
			TabGroupActivity parentActivity = (TabGroupActivity)this.getParent();
			parentActivity.startChildActivity("Project_Message_Detail", previewMessage);
		}
		if (v == set_manager) {
			ProgressBarManager manager=new ProgressBarManager(Project_Info_Member_Detail.this.getParent())
			{

				protected void onPreExecute() {

					Log.v("manager"," in pre execute");

					dialog.setMessage("Loading please wait...");
					dialog.setIndeterminate(true);

					// dialog.setTitle("Manager");
					dialog.show();
				}

				protected Boolean doInBackground(String... params) 
				{

					try {

						IVLE_API_Helper.updateMember(mypref.getString("IVLE_Token", ""), project.getID(),student.getUserID(), "M");
						return true;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return false;
					}


				}
				protected void onPostExecute(Boolean result) {
					this.getDialog().dismiss();
					if (result == false) {
						AlertDialog.Builder builder = new AlertDialog.Builder(Project_Info_Member_Detail.this.getParent());
						builder.setMessage("No internet connection")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}

						});
						AlertDialog alert = builder.create();
						alert.show();
					}
					else {
						set_manager.setVisibility(View.GONE);
						set_member.setVisibility(View.VISIBLE);
					}
				}
			};
			manager.execute();
		}
		if (v == set_member) {
			ProgressBarManager manager=new ProgressBarManager(Project_Info_Member_Detail.this.getParent())
			{

				protected void onPreExecute() {

					Log.v("manager"," in pre execute");

					dialog.setMessage("Loading please wait...");
					dialog.setIndeterminate(true);

					// dialog.setTitle("Manager");
					dialog.show();
				}

				protected Boolean doInBackground(String... params) 
				{

					try {

						IVLE_API_Helper.updateMember(mypref.getString("IVLE_Token", ""), project.getID(),student.getUserID(), "R");
						return true;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return false;
					}


				}
				protected void onPostExecute(Boolean result) {
					this.getDialog().dismiss();
					if (result == false) {
						AlertDialog.Builder builder = new AlertDialog.Builder(Project_Info_Member_Detail.this.getParent());
						builder.setMessage("No internet connection")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}

						});
						AlertDialog alert = builder.create();
						alert.show();
					}
					else {
						set_manager.setVisibility(View.VISIBLE);
						set_member.setVisibility(View.GONE);
					}
				}
			};
			manager.execute();
		}
		if (v == remove) {
			ProgressBarManager manager=new ProgressBarManager(Project_Info_Member_Detail.this.getParent())
			{

				protected void onPreExecute() {

					Log.v("manager"," in pre execute");

					dialog.setMessage("Loading please wait...");
					dialog.setIndeterminate(true);

					// dialog.setTitle("Manager");
					dialog.show();
				}

				protected Boolean doInBackground(String... params) 
				{

					try {

						IVLE_API_Helper.removeMember(mypref.getString("IVLE_Token", ""), project.getID(),student.getUserID());
						return true;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return false;
					}


				}
				protected void onPostExecute(Boolean result) {
					this.getDialog().dismiss();
					if (result == false) {
						AlertDialog.Builder builder = new AlertDialog.Builder(Project_Info_Member_Detail.this.getParent());
						builder.setMessage("No internet connection")
						.setNegativeButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}

						});
						AlertDialog alert = builder.create();
						alert.show();
					}
					else {
						finish();
					}
				}
			};
			manager.execute();



		}
		if (v == back_button) {
			finish();
		}

	}
}
